package com.iotechn.unimall.admin.api.enterprise;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dobbinsoft.fw.core.exception.AdminServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.model.Page;
import com.iotechn.unimall.data.domain.EnterpriseDO;
import com.iotechn.unimall.data.domain.UserDO;
import com.iotechn.unimall.data.dto.EnterpriseDTO;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.EnterpriseMapper;
import com.iotechn.unimall.data.mapper.UserMapper;
import com.iotechn.unimall.data.vo.EnterpriseVO;
import com.iotechn.unimall.data.vo.SelectVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper, EnterpriseDO> implements EnterpriseService {
    @Resource
    private UserMapper userMapper;

    @Override
    public Page<EnterpriseVO> page(Long associationId, String name, Integer page, Integer limit, Long adminId) throws ServiceException {
        return baseMapper.pageEnterprise(Page.div(page, limit, EnterpriseVO.class), associationId,name);
    }

    @Override
    public String create(EnterpriseDTO enterpriseDTO, Long adminId) throws ServiceException {
        EnterpriseDO enterpriseDO = new EnterpriseDO();
        BeanUtil.copyProperties(enterpriseDTO, enterpriseDO);
        if (baseMapper.insert(enterpriseDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return "ok";
    }

    @Override
    public String update(EnterpriseDTO enterpriseDTO, Long adminId) throws ServiceException {
        EnterpriseDO enterpriseDO = new EnterpriseDO();
        BeanUtil.copyProperties(enterpriseDTO, enterpriseDO);
        if (baseMapper.updateById(enterpriseDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return "ok";
    }

    @Override
    public String delete(Long id, Long adminId) throws ServiceException {
        //校验企业是否有关联人员
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserDO::getEnterpriseId, id);
        UserDO userDO = userMapper.selectOne(wrapper);
        if (ObjectUtil.isNotEmpty(userDO)) {
            throw new AdminServiceException(ExceptionDefinition.ENTERPRISE_EXIST_USER);
        }

        QueryWrapper<EnterpriseDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EnterpriseDO::getId, id);
        if (baseMapper.delete(queryWrapper) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return "ok";
    }

    @Override
    public String batchDelete(List ids, Long adminId) throws ServiceException {
        //校验企业是否有关联人员
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(UserDO::getEnterpriseId, ids);
        List<UserDO> userDOS = userMapper.selectList(wrapper);
        if (ObjectUtil.isNotEmpty(userDOS)) {
            throw new AdminServiceException(ExceptionDefinition.ENTERPRISE_EXIST_USER);
        }
        if (baseMapper.deleteBatchIds(ids) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return null;
    }

    @Override
    public String richText(Long id, Long adminId) throws ServiceException {
        QueryWrapper<EnterpriseDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(EnterpriseDO::getId, id);
        EnterpriseDO enterpriseDO = baseMapper.selectOne(wrapper);
        return enterpriseDO.getIntroduction();
    }

    @Override
    public List<SelectVO> enterpriseSelect(Long adminId) throws ServiceException {
        List<EnterpriseDO> enterpriseDOS = baseMapper.selectList(null);
        List<SelectVO> collect = enterpriseDOS.stream().map(o -> {
            SelectVO selectVO = new SelectVO();
            BeanUtil.copyProperties(o, selectVO);
            return selectVO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public EnterpriseDO queryEnterprise(Long id, Long adminId) throws ServiceException {
        QueryWrapper<EnterpriseDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(EnterpriseDO::getId,id);
        return baseMapper.selectOne(wrapper);
    }
}
